<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="650" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="right">

            <el-form-item label="等级名称" prop="name">
				<el-input v-model="form.name" clearable></el-input>
			</el-form-item>
            <el-form-item label="权重" prop="weight">
				<el-input v-model="form.weight" placeholder="数字越大等级越高 , 且不可重复" type='number'></el-input>
			</el-form-item>
			<el-form-item label="达成要求" prop="require">
				<el-checkbox-group v-model="form.require">
                        <div class="chkbox">
							<el-checkbox label="chong">充值满</el-checkbox>
							<el-input v-model="form.recharge_money" class="ipt">
							    <template v-slot:append>元</template>
							</el-input>
						</div>
						<div class="chkbox">
                             <el-checkbox label="consume">消费满</el-checkbox>

							 <el-input v-model="form.consume_money" class="ipt">
							     <template v-slot:append>元</template>
							 </el-input>
						</div>
                </el-checkbox-group>

			</el-form-item>
            <el-form-item label="会员权益" prop="profit">
				<el-checkbox-group v-model="form.profit" style="width: 100%;">


						<div class="chkbox">
							<el-checkbox label="discount">消费享</el-checkbox>

							<el-input v-model="form.discount_num" class="ipt">
							    <template v-slot:append>折</template>
							</el-input>
						</div>
						<div class="chkbox">
							<el-checkbox label="free_paper">餐纸免费</el-checkbox>
						</div>

				</el-checkbox-group>
            </el-form-item>
            <el-form-item label="额外赠送" prop="send">

					<el-checkbox-group v-model="form.send" style="width: 100%;">
						<div class="chkbox">
							<el-checkbox label="send_jifen">送积分</el-checkbox>
							<el-input v-model="form.send_jifen_num" class="ipt">
							    <template v-slot:append>个</template>
							</el-input>
						</div>
						<div class="chkbox">
							<el-checkbox label="send_money">返现金</el-checkbox>
							<el-input v-model="form.send_money_num" class="ipt">
							    <template v-slot:append>元</template>
							</el-input>
						</div>
					</el-checkbox-group>


            </el-form-item>


		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	export default {
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
                cat:[],
				titleMap: {
					add: '新增',
					edit: '编辑',
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {},
				//验证规则
				rules: {
					name: [
						{required: true, message: '请输入名称'}
					],
					weight:[
					    {required: true, message: '请输入权重'}
					]
				}
			}
		},
		mounted() {

		},
		methods: {
            async getcat(){
				var res = await this.$API.system.universal.list.get({},'/desk/area');
				if(res.code == 200){
                        var arr = res.data.rows
                        this.cat =arr
                        this.cat.unshift({'name':'无','value':'','id':''})
				}
			},
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						delete this.form.pname;
						this.isSaveing = true;
                        var res = await this.$API.system.universal.handle.post(this.form,'/user/level_save');
						this.isSaveing = false;
						if(res.code == 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				this.form = data
			}
		}
	}
</script>

<style>
.chkbox{
    margin-bottom: 10px;
    display: inline-block;
	width: 100%;
}
.ipt{
	width: 300px;
	margin-left:8px
}
</style>
